package com.test.services;

import java.util.ArrayList;

import org.apache.http.HttpException;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class EmailSenderService {

	private static final String TAG = "EmailSenderService";
	String result;
	
	public String SendEmail(String username, String emailaddress) {

		final String un = username;
		final String ea = emailaddress;

		ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
		postParameters.add(new BasicNameValuePair("username", un));
		Log.d(TAG, "Post Parameter Username " + un);
		postParameters.add(new BasicNameValuePair("emailaddress", ea));
		Log.d(TAG, "Post Parameter EmailAddress " + ea);

		// String valid = "1";
		String response = null;
		try {
			response = HttpClientService.executeHttpPost(
					"http://blackvault.net63.net/PHPMailer_5.2.1/emailer.php", postParameters);

			Log.d(TAG, "response " + response);
			result = response.toString();
			result = result.replaceAll("\\s+", "");
			result = result.substring(0, 1);
			Log.d(TAG, "res after substring " + result);

			if (result.equals("1")) {

				Log.d(TAG, "Email Sent");

			} else {

				Log.d(TAG, "Email not sent");

			}

		} catch (HttpException ex) {
			Log.e(TAG, "HTTP error " + ex.toString());
		} catch (Exception e) {

			Log.e(TAG, "Response Error " + e.toString());
		}

		Log.d(TAG, "Returning res " + result);
		return result;
	}
	
}
